#!/usr/bin/env python3
import PyPDF2
import sys
import os

try:
    f = PyPDF2.PdfReader(sys.argv[1])
    top = int(sys.argv[2])
    buttom = int(sys.argv[3])
    left = int(sys.argv[4])
    right = int(sys.argv[5])
except:
    f = PyPDF2.PdfReader(input("输入外文原文的文件路径："))
    top = int(input("输入裁剪的上边距："))
    buttom = int(input("输入裁剪的下边距："))
    left = int(input("输入裁剪的左边距："))
    right = int(input("输入裁剪的右边距："))

if not os.path.exists("外文原文"):
    os.mkdir("外文原文")

n = f.getNumPages()

for i in range(n):
    page = f.getPage(i)
    page.cropBox.top -= top
    page.cropBox.bottom = buttom
    page.cropBox.left = left
    page.cropBox.right -= right
    w = PyPDF2.PdfWriter()
    w.add_page(page)
    w.write("外文原文/{}.pdf".format(i+1))

texfile = open("外文原文.tex", "wt")
for i in range(n):
    texfile.write("\\setlength{\\parindent}{0pt}\n")
    texfile.write("\\includegraphics[width=\\textwidth]{外文原文/" + str(i+1) + ".pdf}\n")
    texfile.write("\\newpage\n")
